﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Inspection.InspectionManage;
using Camc.Quality.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.Inspection
{
    public class InspectionExportAppService : EpPlusExcelExporterBase
    {
        private readonly ITempFileCacheManager _tempFileCacheManager;
        public InspectionExportAppService(ITempFileCacheManager tempFileCacheManager) : base(tempFileCacheManager)
        {
            _tempFileCacheManager = tempFileCacheManager;
        }
        public string ExportToFile(List<InspectionStandardDescOutDto> inspectionReportDataOutDto)
        {
            return CreateExcelPackageLocal(
                "异常记录导出.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add(L("异常记录"));
                    sheet.OutLineApplyStyle = true;

                    AddHeader(
                        sheet,
                        L("异常单号"),
                        L("一级异常"),
                        L("二级异常"),
                        L("异常等级"),
                        L("异常描述"),
                        L("创建时间"),
                        L("提报人"),
                        L("责任部门"),
                        L("处理人"),
                        L("原因分析"),
                        L("处理措施"),
                        L("处理完成时间"),
                        L("确认时间"),
                        L("目前状态")
                        );

                    AddObjects(
                        sheet, 2, inspectionReportDataOutDto,
                        _ => _.Code,
                        _ => _.InspectionTypeDesc,
                        _ => _.InspectionItemInfo,
                        _ => _.InspectionSeverityDesc,
                        _ => _.InspectionDetails,
                        _ => _.CreationTime,
                        _ => _.Submitter,
                        _ => _.OrganizationUnits,
                        _ => _.Handler,
                        _ => _.CauseAnalysis,
                        _ => _.TreatmentMeasures,
                        _ => _.CauseAnalysis,
                        _ => _.CompleteTime,
                        _ => _.ConfirmTime,
                        _ => (States)_.State
                        );
                    //Formatting cells

                    for (var i = 1; i <= 15; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }
    }
}
